//
//  MainView.m
//  Comma
//
//  Created by 刘轩博 on 15/1/19.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MainView.h"


#define PHOTO_WIDTH 100
#define PHOTO_X (SCREEN_WIDTH - 148 - PHOTO_WIDTH) / 2 + 74
@implementation MainView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createLogoImage];
        [self createPhotoButton];
        [self createCameraOrAmbulButton];
    }
    return self;
}

#pragma mark - 创建logo图片
- (void)createLogoImage
{
    self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 95.5) / 2, 108, 95.5, 121.5)];
    if (iPhone5) {
        _imgView.frame = CGRectMake((SCREEN_WIDTH - 81) / 2, 91.8, 81, 103);
    }
    [_imgView setImage:[UIImage imageNamed:@"logo.pdf"]];
    
    //    [self addSubview:_backView];
    //    self.resetLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 140, SCREEN_WIDTH, 44)];
    //    [_resetLabel setText:@"重置密码"];
    //    [_resetLabel setTextAlignment:NSTextAlignmentCenter];
    //    [_resetLabel setFont:[UIFont systemFontOfSize:40]];
    //    [_resetLabel setTextColor:TEXTERGRAY];
    //    [self addSubview:resetLabel];
    self.iconView = [[UILabel alloc] initWithFrame:CGRectMake(0, 123, SCREEN_WIDTH, 121.5)];
    if (iPhone5) {
        _iconView.frame = CGRectMake(0, 106.8, SCREEN_WIDTH, 44);
    }
    [_iconView setAlpha:0];
    [_iconView setText:@"重置密码"];
    [_iconView setTextAlignment:NSTextAlignmentCenter];
    [_iconView setFont:[UIFont systemFontOfSize:28]];
    [_iconView setTextColor:TEXTERGRAY];
    [self addSubview:_iconView];
    
    self.iconConView = [[UILabel alloc] initWithFrame:CGRectMake(0, 123, SCREEN_WIDTH, 121.5)];
    if (iPhone5) {
        _iconConView.frame = CGRectMake(0, 106.8, SCREEN_WIDTH, 44);
    }
    [_iconConView setAlpha:0];
    [_iconConView setText:@"重置密码成功"];
    [_iconConView setTextAlignment:NSTextAlignmentCenter];
    [_iconConView setFont:[UIFont systemFontOfSize:28]];
    [_iconConView setTextColor:TEXTERGRAY];
    [self addSubview:_iconConView];
    
    [self addSubview:_imgView];
}

#pragma mark - 创建上传头像按钮
- (void)createPhotoButton
{
    self.photoButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _photoButton.frame = CGRectMake(PHOTO_X + SCREEN_WIDTH, 116, PHOTO_WIDTH, PHOTO_WIDTH);
    _photoButton.layer.cornerRadius = PHOTO_WIDTH / 2;
    _photoButton.layer.masksToBounds = YES;
    if (iPhone5) {
        _photoButton.frame = CGRectMake((SCREEN_WIDTH - 85) / 2 + SCREEN_WIDTH, 89, 85, 85);
        _photoButton.layer.cornerRadius = 85 / 2;
    }
    [_photoButton setTitle:@"头像 +" forState:UIControlStateNormal];
    [_photoButton setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [_photoButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0f]];
    // 设置按钮线的样式
    _photoButton.layer.borderWidth = 0.4;
    _photoButton.layer.borderColor = LINEERGRAY.CGColor;    
    // 背景颜色样式
    [_photoButton setBackgroundColor:FOOTERGRAY];
    
    // 添加点击事件
    [_photoButton addTarget:self action:@selector(photoSelectAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_photoButton];
}

#pragma mark - 创建选择相机&相册按钮
- (void)createCameraOrAmbulButton
{
    // 模糊层
    self.blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _blackView.backgroundColor = [UIColor blackColor];
    _blackView.alpha = 0.6;
    [_blackView resignFirstResponder];
    
    // 承载按钮的View
    self.photoChildView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 88)];
    _photoChildView.backgroundColor = [UIColor whiteColor];
    
    // 相机按钮
    self.camera = [UIButton buttonWithType:UIButtonTypeSystem];
    _camera.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
    [_camera setTitle:@"相机" forState:UIControlStateNormal];
    [_camera setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [_camera.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0]];

    [self.photoChildView addSubview:_camera];
    
    // 相册按钮
    self.album = [UIButton buttonWithType:UIButtonTypeSystem];
    _album.frame = CGRectMake(0, 44, SCREEN_WIDTH, 44);
    [_album setTitle:@"相册" forState:UIControlStateNormal];
    [_album setTitleColor:TEXTGREENCOLOR forState:UIControlStateNormal];
    [_album.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0]];

    [self.photoChildView addSubview:_album];
    
    //分割线
    UIView *buttonLine = [[UIView alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, 0.5)];
    buttonLine.backgroundColor = DIVIDINGLINECOLOR;
    [_photoChildView addSubview:buttonLine];
    
}

#pragma mark - 选择头像
- (void)photoSelectAction:(UIButton *)button
{ 
    [self addSubview:self.blackView];
    [self addSubview:_photoChildView];
    
    [UIView beginAnimations:@"viewAppear" context:nil];
    [UIView setAnimationDuration:0.2];
    self.photoChildView.frame = CGRectMake(0, SCREEN_HEIGHT - 88, SCREEN_WIDTH, 88);
    [UIView commitAnimations];
    
}

// 回收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:0.3 animations:^{
        self.photoChildView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 88);
        [self.blackView removeFromSuperview];
    }];
    [self endEditing:YES];
}


@end
